<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\SysConfig;
use App\Models\User;
use App\Utils\CodeUtil;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\URL;

class LoginController extends Controller
{
    public function index()
    {
        return view('Api/login/index');
    }

    public function login(Request $request)
    {
//        $rules = ['captcha' => 'required|captcha'];
//        $validator = Validator::make($request->all(), $rules);
//        if($validator->fails()) return response()->json(['code' => CodeUtil::API_ERROR, "msg" => "验证码错误","url" => URL::action('Api\LoginController@index')]);
        $name = $request->input('name');
        $pass = $request->input('password');
        $user = User::where("name", $name)->first();
        if (empty($user)) return response()->json(['code' => CodeUtil::API_ERROR, "msg" => "账号或密码错误", "url" => URL::action('Api\LoginController@index')]);
        if (!Hash::check($pass, $user["password"])) return response()->json(['code' => CodeUtil::API_ERROR, "msg" => "账号或密码错误", "url" => URL::action('Api\LoginController@index')]);
        //$request->session()->put("uid",$user->id);
        session(['admin_user' => $user]);
        return response()->json(['code' => CodeUtil::SUCCESS, "msg" => "登陆成功!", "url" => URL::action('Api\IndexController@index')]);
    }

    //退出登录
    public function exitLogin()
    {
        session(['admin_user' => '']);
        return redirect('api/login');
    }
    public function getVerifyCode()
    {
        return captcha();
    }
}
